package com.zbcbms.service.impl;

import com.zbcbms.dao.BookBorrowDao;
import com.zbcbms.dao.BookDao;
import com.zbcbms.dao.impl.BookBorrowDaoImpl;
import com.zbcbms.dao.impl.BookDaoImpl;
import com.zbcbms.entity.dto.AddBookBorrowDto;
import com.zbcbms.entity.chart.BookBorrowChart;
import com.zbcbms.entity.tableData.BookBorrowTableData;
import com.zbcbms.entity.vo.BookBorrowVo;
import com.zbcbms.service.BookBorrowService;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.List;

/**
 * @author fengq
 */
public class BookBorrowServiceImpl implements BookBorrowService {


    private BookBorrowDao bookBorrowDao=new BookBorrowDaoImpl();


    private BookDao bookDao=new BookDaoImpl();

    @Override
    public Boolean saveOne(AddBookBorrowDto entity) {
        bookDao.updateStatusByNumber(entity.getBookNumber(),"使用中");
        return  bookBorrowDao.insertOne(entity);
    }

    @Override
    public Boolean updateById(AddBookBorrowDto entity) {
         if (entity.getStatus().equals("已归还")){
             bookDao.updateStatusByNumber(entity.getBookNumber(),"空闲");
         }else if (entity.getStatus().equals("借用中")){
             bookDao.updateStatusByNumber(entity.getBookNumber(),"使用中");
         }
        return bookBorrowDao.update(entity);
    }

    @Override
    public Boolean deleteById(String id) {
        return bookBorrowDao.deleteById(id);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return bookBorrowDao.deleteByIds(ids);
    }

    @Override
    public Boolean pjySicsDeleteById(String id) {
        return bookBorrowDao.pjySicsDeleteById(id);
    }

    @Override
    public Boolean recoverById(String id) {
        return bookBorrowDao.recoverById(id);
    }

    @Override
    public ObservableList<BookBorrowTableData> getBorrowRecordByUser(String userNumber) {
        List<BookBorrowVo> bookBorrowVos = bookBorrowDao.selectBorrowByUser(userNumber);
        ObservableList<BookBorrowTableData> list= FXCollections.observableArrayList();
        for (BookBorrowVo bookBorrow :bookBorrowVos){
            BookBorrowTableData bookBorrowTableData = new BookBorrowTableData(bookBorrow.getId(), bookBorrow.getBookNumber(), bookBorrow.getBookName(),
                    bookBorrow.getBorrowDate(), bookBorrow.getReturnDate(), bookBorrow.getDeadline(), bookBorrow.getStatus(),
                    bookBorrow.getOperatorName(), bookBorrow.getUserNumber(), bookBorrow.getUserName());
            list.add(bookBorrowTableData);
        }
        return list;
    }

    @Override
    public ObservableList<BookBorrowTableData> getAllBookBorrowTable() {
        List<BookBorrowVo> bookBorrowVos = bookBorrowDao.selectAllBorrow();
        ObservableList<BookBorrowTableData> list= FXCollections.observableArrayList();
         for (BookBorrowVo bookBorrow :bookBorrowVos){
             BookBorrowTableData bookBorrowTableData = new BookBorrowTableData(bookBorrow.getId(), bookBorrow.getBookNumber(), bookBorrow.getBookName(),
                     bookBorrow.getBorrowDate(), bookBorrow.getReturnDate(), bookBorrow.getDeadline(), bookBorrow.getStatus(),
                     bookBorrow.getOperatorName(), bookBorrow.getUserNumber(), bookBorrow.getUserName());
             list.add(bookBorrowTableData);
        }
         return list;
    }

    @Override
    public long getBorrowCountByTime(String date) {
        return bookBorrowDao.selectBorrowCountByTime(date);
    }

    @Override
    public long getReturnCountByTime(String date) {
        return bookBorrowDao.selectReturnCountByTime(date);
    }

    @Override
    public List<BookBorrowChart> getBorrowByBookName() {
        return bookBorrowDao.selectCountByBookName();
    }

    @Override
    public ObservableList<BookBorrowTableData> getDeletedBorrow() {
        List<BookBorrowVo> bookBorrowVos = bookBorrowDao.selectAllDeletedBorrow();
        return conversion(bookBorrowVos);
    }

    @Override
    public ObservableList<BookBorrowTableData> getBookBorrowVoByKeyword(String keyword) {
        List<BookBorrowVo> bookBorrowVos = bookBorrowDao.selectBookBorrowVoByKeyword(keyword);
        return conversion(bookBorrowVos);
    }

    @Override
    public ObservableList<BookBorrowTableData> getBookBorrowVoByKeywordAndUserNumber(String keyword, String userNumber) {
        List<BookBorrowVo> bookBorrowVos = bookBorrowDao.selectBookBorrowVoByKeywordAndUserNumber(keyword,userNumber);
        return conversion(bookBorrowVos);
    }

    @Override
    public ObservableList<BookBorrowTableData> getBookBorrowByUserAndDay(String userNumber, String days) {
        List<BookBorrowVo> bookBorrowVos = bookBorrowDao.selectBookBorrowByUserAndDayNums(userNumber, days);
        return conversion(bookBorrowVos);
    }

    public ObservableList<BookBorrowTableData> conversion(List<BookBorrowVo> bookBorrowVos){
        ObservableList<BookBorrowTableData> list= FXCollections.observableArrayList();
        for (BookBorrowVo bookBorrow :bookBorrowVos){
            BookBorrowTableData bookBorrowTableData = new BookBorrowTableData(bookBorrow.getId(), bookBorrow.getBookNumber(), bookBorrow.getBookName(),
                    bookBorrow.getBorrowDate(), bookBorrow.getReturnDate(), bookBorrow.getDeadline(), bookBorrow.getStatus(),
                    bookBorrow.getOperatorName(), bookBorrow.getUserNumber(), bookBorrow.getUserName());
            list.add(bookBorrowTableData);
        }
        return list;
    }

}
